/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.common.util;

import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-07-07 14:35
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-07-07
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class CapMathUtil {

    /**
     * Double类型求和
     * @param collection
     * @return
     */
    public static Double averageOfDouble(Collection<Double> collection) {
        Double result = 0D;
        if (CollectionUtils.isEmpty(collection)) {
            return result;
        }
        for (Double item : collection) {
            result += Objects.nonNull(item) ? item : 0D;
        }
        if(result == 0D){
            return 0D;
        }
        return result / collection.size();
    }

    public static Double maxOfDouble(List<Double> list) {
        list.sort(Double::compare);
        return list.get(list.size()-1);
    }

    public static Double minOfDouble(List<Double> list) {
        list.sort(Double::compare);
        return list.get(0);
    }
}
